﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;

public partial class insurance_ordereditor : MemberPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        PageBegin();

        /// 保存设置
        if (RequestBase.GetFormString("___EVENTTARGET") == "submitorder")
        {
            SaveOrder();
        }

        if (!IsPostBack)
        {
            InitObj();
        }
    }

    #region 获取POST值初始化控件
    void InitObj()
    { 
        Model.Insurance.InsurancePolicyBase _obj = BLL.Insurance.InsurancePolicyBase.GetObjById(HttpUtility.UrlDecode(RequestBase.GetQueryString("objectid")));

        PolicyObj = _obj; 
        
        object _code = BLL.Search.CommonSearch.ExecScalar("EnumItem", "GroupCode",
                          new string[] { "EnumKey", "EnumValue" },
                          new string[] { "Equal", "Equal" },
                          new string[] { "'DestCountryName'", "'"+_obj.DestCountryName+"'" },
                          new string[] { "SortIndex" },
                          new string[] { "ASC" });

        if (_code != null)
            txtAreaCode.Value = _code.ToString();
    }
    void PolicyholdersListDataBind(Model.Insurance.InsurancePolicyBase _obj)
    {
        string[] fields = new string[] { "Name", "NameEn", "IDType", "IDNo", "Sex", "Birthday", "Age" };
        string[] fieldstype = new string[] { "System.String", "System.String", "System.String", "System.String", "System.String", "System.String", "System.String" };
        DataTable dat = (new DataTableUtils()).BuildingDTStruct("PolicyholdersDAT", fields, fieldstype);

        for (int i = 0; i <= _obj.IDNo.Split(',').Length - 1; i++)
        {
            dat.Rows.Add(new object[] { 
                _obj.Name.Split(',')[i].Trim(), 
                _obj.EName.Split(',')[i].Trim(), 
                BLL.Sys.EnumItem.GetTextByValue("IDType", _obj.IDType.Split(',')[i].Trim()), 
                _obj.IDNo.Split(',')[i].Trim(),
                _obj.Sex.Split(',')[i].Trim().Replace("1", "女").Replace("0", "男"),
                _obj.Birtyday.Split(',')[i].Trim(),
                _obj.Age.Split(',')[i].Trim()
            });
        }

        rptPolicyholders.DataSource = dat;
        rptPolicyholders.DataBind();
    }
    #endregion

    #region 获取订单信息
    Model.Insurance.InsurancePolicyBase PolicyObj
    {
        get
        {
            Model.Insurance.InsurancePolicyBase _obj = new Model.Insurance.InsurancePolicyBase();

            _obj.InsurancePolicyId = RequestBase.GetFormString("txtObjectId");

            _obj.ProductItemId = RequestBase.GetFormString("txtProductItemId");
            _obj.ProductId = RequestBase.GetFormString("selProductList");
            _obj.PassCountry = RequestBase.GetFormString("txtPassCountryCode");
            _obj.DestCountryName = RequestBase.GetFormString("selDestinationList");
            _obj.Email = RequestBase.GetFormString("txtEmail");

            _obj.TName = RequestBase.GetFormString("txtTName");
            _obj.ETName = RequestBase.GetFormString("txtTNameEn");
            _obj.TBirtyday = DateTime.Parse(string.IsNullOrEmpty(txtTBirthday.Value.Trim()) ? "1754-01-01" : txtTBirthday.Value.Trim());
            _obj.TSex = RequestBase.GetFormString("selTSex");
            _obj.TIDType = RequestBase.GetFormString("selTIDType");
            _obj.TIDNo = RequestBase.GetFormString("txtTIDNo");
            _obj.TAge = RequestBase.GetFormString("txtTAge");
            _obj.Name = RequestBase.GetFormString("txtName");
            _obj.EName = RequestBase.GetFormString("txtNameEn");
            _obj.Birtyday = RequestBase.GetFormString("txtBirthday");
            _obj.Sex = RequestBase.GetFormString("selSex");
            _obj.Age = RequestBase.GetFormString("txtAge");
            _obj.IDType = RequestBase.GetFormString("selIDType");
            _obj.IDNo = RequestBase.GetFormString("txtIDNo");
            _obj.Phone = RequestBase.GetFormString("txtPhone");
            _obj.CJType = RequestBase.GetFormString("ddPolicyType"); 
            _obj.GetStartDate = DateTime.Parse(RequestBase.GetFormString("txtStartDate"));
            _obj.EndDate = DateTime.Parse(RequestBase.GetFormString("txtEndDate")); 
            _obj.Invoice = (RequestBase.GetFormString("rdoInvoice") == "1"); 
            _obj.InvoiceRecipient = RequestBase.GetFormString("txtInvoiceRecipient");
            _obj.InvoicePhone = RequestBase.GetFormString("txtInvoicePhone");
            _obj.InvoiceTitle = RequestBase.GetFormString("txtInvoiceTitle");
            _obj.InvoiceAddress = RequestBase.GetFormString("txtInvoiceAddress");
            _obj.Price = decimal.Parse(RequestBase.GetFormString("txtPrice"));
            _obj.PersonQuantity = int.Parse(RequestBase.GetFormString("txtNoOfTravellers"));
            _obj.TotalAmount = _obj.Price * _obj.PersonQuantity;
            _obj.OrderDate = DateTime.Now;
            _obj.CreatedOn = DateTime.Now;
            _obj.ModifyOn = DateTime.Now;
            _obj.CancelDate = DateTime.MaxValue; 
            _obj.CustomerId = this.UserId;
            _obj.ExpressType = RequestBase.GetFormString("selExpressType");
            _obj.SalesTypeCode = int.Parse(RequestBase.GetFormString("rdoSalesTypeCode"));

            /// 需要发票或纸质保单
            if (_obj.Invoice || _obj.SalesTypeCode == 2)
                _obj.InvoiceFee = decimal.Parse(RequestBase.GetFormString("selExpressType").Replace("顺丰快递", "20.00").Replace("申通快递", "10.00"));
            else
                _obj.InvoiceFee = 0;

            return _obj;
        }
        set
        {
            if (value != null)
            {
                txtObjectId.Value = value.InsurancePolicyId;
                txtProductItemId.Value = value.ProductItemId;
                txtOrgProductItemId.Value = value.ProductItemId;
                txtProductId.Value = value.ProductId;
                txtPassCountryCode.Value = value.PassCountry;
                txtDestCountryCode.Value = value.DestCountryName;  
                txtEmail.Value = value.Email;
                txtTName.Value = value.TName;
                txtTNameEn.Value = value.ETName;
                txtTBirthday.Value = value.TBirtyday.ToString("yyyy-MM-dd").Replace("1754-01-01", "");
                txtTSex.Value = value.TSex;
                txtTIDType.Value = value.TIDType;
                txtTIDNo.Value = value.TIDNo;
                txtTAge.Value = value.TAge;
                txtNameOrg.Value = value.Name;
                txtNameEnOrg.Value = value.EName;
                txtBirthdayOrg.Value = value.Birtyday;
                txtSexOrg.Value = value.Sex;
                txtIDTypeOrg.Value = value.IDType;
                txtIDNoOrg.Value = value.IDNo;
                txtAgeOrg.Value = value.Age;
                txtPhone.Value = value.Phone;
                txtCJType.Value = value.CJType;
                txtStartDate.Value = value.GetStartDate.ToString("yyyy-MM-dd");
                txtEndDate.Value = value.EndDate.ToString("yyyy-MM-dd");

                if (value.Invoice)
                    rdoInvoice_1.Checked = true;
                else
                    rdoInvoice_0.Checked = true;

                if (value.SalesTypeCode == 1)
                    rdoSalesTypeCode_1.Checked = true;
                else
                    rdoSalesTypeCode_2.Checked = true;

                txtInvoiceRecipient.Value = value.InvoiceRecipient;
                txtInvoicePhone.Value = value.InvoicePhone;
                txtInvoiceTitle.Value = value.InvoiceTitle;
                txtInvoiceAddress.Value = value.InvoiceAddress; 
                txtPrice.Value = value.Price.ToString();
                selExpressType.Value = value.ExpressType;

                /// 需要发票或纸质保单
                if (value.Invoice || value.SalesTypeCode == 2)
                    txtInvoiceFee.Value = value.ExpressType.Replace("顺丰快递", "20.00").Replace("申通快递", "10.00");
                else
                    txtInvoiceFee.Value = "0";

                txtTotalAmount.Value = value.TotalAmount.ToString();
                txtNoOfTravellers.Value = value.PersonQuantity.ToString();
                ddlNoOfTravellers.Value = value.PersonQuantity.ToString();

                PolicyholdersListDataBind(value);
            }
        }
    }
    #endregion 

    #region 保存数据

    void SaveOrder()
    {
        string result = BLL.Insurance.InsurancePolicyBase.InsurancePolicyAdmin(PolicyObj);
        if (result.Split('|')[0] == "succeeded")
        {
            JScript.AlertAndRedirect("保存成功!", "insurance_orderlist.aspx");
        }
        else
        {
            divErrorMsg.Style.Add("display", "");
            divErrorMsg.InnerHtml = string.Format(divErrorMsg.InnerHtml, "<li style=\"background: none;\">" + result + "</li>");
        }
    }
    #endregion
}